function showNewPhotoForm() {
	listFolder();
    $('#newPhotoForm').jqmShow();
}

function savePhoto() {
    try {
        ajaxFileUpload();
    } catch (e) {
        alert(e);
    }
}

function addPhotoToList(photo) {
	$('<li><a class="thumb" href="' + getShowSlideshowUrl(photo.id) + '" title="' + photo.name + '"><img src="' + getShowThumbnailUrl(photo.id) + '" alt="' + photo.name + '"/></a><div class="caption">' + photo.name + '</div></li>')
	.appendTo($('#photoList ul'));
}

function addPhotoList(photos, total) {
	$('#photoList').empty();
	$('<ul class="thumbs noscript"></ul>').appendTo($('#photoList'));
	if (photos) {
		$.each(photos, function() {
			addPhotoToList(this);
		});
		if (!gallerific_inited) {
			initGallerific(total);
		}
	}
}

function listPhoto() {
	$.ajax({
		url : getPhotoListUrl(),
		dataType : 'json',
		timeout : 5000,
		success : function(json) {
			if (json.success) {
				addPhotoList(json.photos);
			} else {
			    alert(json.error);
			}
		},
		error : function(request, status, error) {
			alert('error : ' + request.responseText);
		}	
	});
}



function ajaxFileUpload() {
	//starting setting some animation when the ajax starts and completes
	$("#loading").ajaxStart(function(){
		$(this).show();
	}).ajaxComplete(function(){
		$(this).hide();
	});
	
	/*
	prepareing ajax file upload
	url: the url of script file handling the uploaded files
                   fileElementId: the file type of input element id and it will be the index of  $_FILES Array()
	dataType: it support json, xml
	secureuri:use secure protocol
	success: call back function when the ajax complete
	error: callback function when the ajax failed
	*/
	$.ajaxFileUpload({
		url : getPhotoAdminUrl(), 
		secureuri : false,
		fileElementId : 'photoFile',
		dataType : 'json',
		data : {'save' : '', 'photo.name' : $('#photo_name').attr('value'), 'photo.folder' : $($('#photo_folder option:selected')[0]).attr('value')},
		success : function (data, status){
			if(data.success){
				$('#newPhotoForm').jqmHide();
				addPhotoToList(data.photo);
			} else {
			    alert(data.error);
			}
		},
		error : function (request, status, e){
			alert('error : ' + resquest.responseText);
		}
	});
	
	return false;
}

function deleteAllPhotos() {
    $.ajax({
        url : getDeleteAllPhotosUrl(),
        success : function() {
    	   alert('Operation success!');
    	   listPhoto();
        },
        error: function (request, status, e){
        	alert('error : ' + resquest.responseText);
        }
	});
}

function deleteAllFolders() {
    $.ajax({
        url : getDeleteAllFoldersUrl(),
        success : function() {
           alert('Operation success!');
           listPhoto();
        },
        error: function (request, status, e){
            alert('error : ' + resquest.responseText);
        }
    });
}

function showNewFolderForm() {
    $('#newFolderForm').jqmShow();
}

function saveNewFolder() {
    $.ajax({
        url : getFolderAdminUrl(),
        data : {'save' : '', 'folder.name' : $('#folder_name').attr('value')},
        dataType : 'json',
        success : function(data) {
	        if (data.success) {
	            $('#newFolderForm').jqmHide();
	            $('<option value="' + data.folder.id + '">' + data.folder.name + '</option>').appendTo($('#photo_folder'));
	        } else {
	            alert(data.error);
	        }
        },
        error : function (request, status, e) {
        	alert('error : ' + resquest.responseText);
        }
	});
}

function listFolder(selectElm) {
	if (selectElm === undefined) {
	    selectElm = '#photo_folder';
	}
	$.ajax({
        url : getFolderListUrl(),
        dataType : 'json',
        success : function(data) {
            if (data.success) {
            	$('#photo_folder').empty();
            	if (data.folders) {
                    $.each(data.folders, function() {
                        $('<option value="' + this.id + '">' + this.name + '</option>').appendTo($(selectElm));
                    });
            	}
            } else {
                alert(data.error);
            }
        },
        error : function (request, status, e) {
        	alert('error : ' + resquest.responseText);
        }
    });
}

function listFolder2() {
	$.ajax({
        url : getFolderListUrl(),
        dataType : 'json',
        success : function(data) {
            if (data.success) {
            	$('#folderList').empty();
            	if (data.folders) {
                    $.each(data.folders, function() {
                    	var cover = '<img src="/folder?cover=&folder=' + this.id + '" width="150" height="150"/>';
                        $('<div style="width: 150px; float: left; margin: 20px;"><div class="folder" onclick="showFolder(\'' + this.id + '\')" id="folder-' + this.id + '" onmouseover="$(this).addClass(\'folderHover\')" onmouseout="$(this).removeClass(\'folderHover\')">' + /*this.name +*/ cover + '</div>' + this.name + '</div>').appendTo($('#folderList'));
                    });
            	}
            	$('<div class="clear"></div>').appendTo($('#folderList'));
            } else {
                alert(data.error);
            }
        },
        error : function (request, status, e) {
        	alert('error : ' + resquest.responseText);
        }
    });
}

function showFolder(folderId) {
	document.location.href = getShowFolderUrl(folderId);
}
function findPhotosByFolder() {
	$.ajax({
        url : getFindPhotosByFolderUrl($($('#filteredFolder option:selected')[0]).attr('value')),
        dataType : 'json',
        timeout : 5000,
        success : function(json) {
            if (json.success) {
                $('#photoList').empty();
                if (json.photos) {
                    $.each(json.photos, function() {
                        addPhotoToList(this);
                    });
                    $('<ul class="filmstrip"></ul>').appendTo($('#photoList'));
                }
            } else {
                alert(json.error);
            }
        },
        error : function(request, status, error) {
            alert('error : ' + request.responseText);
        }   
    });
}

function securityCheck() {
    $.ajax({
        url : 'security?check=',
        dataType : 'json',
        timeout : 5000,
        success : function(json) {
    	   if (json.success) {
    		    $('<a href="' + json.logout + '">Log out</a>').appendTo($('#loggedIn'));
    		    $('#loggedIn').show();
    	   } else {
    		   $('<a href="' + json.login + '">Log in</a>').appendTo($('#loggedOut'));
               $('#loggedOut').show();
    	   }
    	}
	});
}